local fushan = fk.CreateSkill {
  name = "fushan",
}

local fushan_func={
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) and player.phase == Player.Play then
      if event == fk.EventPhaseStart then
        return not table.every(player.room:getOtherPlayers(player, false), function(p) return p:isNude() end)
      else
        if player:getMark("@fushan-phase") == 0 then return end
        local card = Fk:cloneCard("slash")
        local skill = card.skill
        local n = skill:getMaxUseTime(player, Player.HistoryPhase, card, nil)
        if not n or player:usedCardTimes("slash", Player.HistoryPhase) < n then
          if table.every(player:getMark("fushan-phase"), function(id) return not player.room:getPlayerById(id).dead end) then
            return true
          end
        end
        return player:getHandcardNum() < player.maxHp
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(self.name)
    if event == fk.EventPhaseStart then
      room:notifySkillInvoked(player, self.name, "special")
      local targets = table.filter(room:getOtherPlayers(player, false), function(p) return not p:isNude() end)
      if #targets == 0 then return end
      room:doIndicate(player.id, table.map(targets, Util.IdMapper))
      local mark = {}
      for _, p in ipairs(targets) do
        if player.dead then return end
        if not p.dead and not p:isNude() then
          local cards = room:askForCard(p, 1, 1, true, self.name, true, nil, "#fushan-give:"..player.id)
          if #cards > 0 then
            room:moveCardTo(Fk:getCardById(cards[1]), Card.PlayerHand, player, fk.ReasonGive, self.name, nil, false, p.id)
            room:addPlayerMark(player, "@fushan-phase", 1)
            table.insert(mark, p.id)
          end
        end
      end
      if #mark > 0 then
        room:setPlayerMark(player, "fushan-phase", mark)
      end
    else
      local card = Fk:cloneCard("slash")
      local skill = card.skill
      local n = skill:getMaxUseTime(player, Player.HistoryPhase, card, nil)
      if not n or player:usedCardTimes("slash", Player.HistoryPhase) < n then
        if table.every(player:getMark("fushan-phase"), function(id) return not room:getPlayerById(id).dead end) then
          room:notifySkillInvoked(player, self.name, "negative")
          room:loseHp(player, 2, self.name)
          return
        end
      end
      room:notifySkillInvoked(player, self.name, "drawcard")
      player:drawCards(player.maxHp - player:getHandcardNum(), self.name)
    end
  end,
}
fushan:addEffect(fk.EventPhaseStart, fushan_func)
fushan:addEffect(fk.EventPhaseEnd, fushan_func)
fushan:addEffect("targetmod", {
  residue_func = function(self, player, skill, scope)
    if skill.trueName == "slash_skill" and player:getMark("@fushan-phase") > 0 and scope == Player.HistoryPhase then
      return player:getMark("@fushan-phase")
    end
  end,
})

return fushan